using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace QuestFramework.UI
{
    public class ControllerInputPrompt : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch sprites;
        SpriteFont promptFont;
        int width;
        int height;
        Vector2 textSize;
        private string promptText;
        private Texture2D buttonImage;
        private Texture2D buttonYImage;
        private Texture2D buttonAImage;
        private bool isHidden;
        private byte alpha;
        private bool startDrainingAlpha;
        private bool startFillingAlpha;
        private Buttons buttonToPrompt;

        public ControllerInputPrompt(Game game, Buttons displayButton)
            : base(game)
        {
            buttonToPrompt = displayButton;
            this.DrawOrder = 99;
        }

        public override void Initialize()
        {
            sprites = new SpriteBatch(Game.GraphicsDevice);
            promptFont = Game.Content.Load<SpriteFont>("Fonts\\Pericles");
            buttonYImage = Game.Content.Load<Texture2D>("Textures\\button_y");
            buttonAImage = Game.Content.Load<Texture2D>("Textures\\button_a");

            switch (buttonToPrompt)
            {
                case Buttons.A:
                    buttonImage = buttonAImage;
                    break;
                case Buttons.Y:
                    buttonImage = buttonYImage;
                    break;
                default:
                    buttonImage = buttonYImage;
                    break;
            }

            width = Game.GraphicsDevice.Viewport.Width;
            height = Game.GraphicsDevice.Viewport.Height;
            promptText = "Enter Building (Ignore Image, Press Y)";
            textSize = promptFont.MeasureString(promptText);
            isHidden = false;
            alpha = 0;
            startFillingAlpha = true;
            startDrainingAlpha = false;

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (startFillingAlpha)
            {
                alpha += 1;

                if (alpha >= 250)
                {
                    alpha = 255;
                    startFillingAlpha = false;
                    startDrainingAlpha = false;
                    isHidden = false;
                }
            }
            
            if (startDrainingAlpha)
            {
                alpha -= 1;

                if (alpha <= 5)
                {
                    alpha = 0;
                    startDrainingAlpha = false;
                    startFillingAlpha = false;
                    isHidden = true;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (!isHidden)
            {
                sprites.Begin();
                sprites.DrawString(promptFont, promptText, new Vector2((width - textSize.X) / 2, height - 60)
                    , new Color(0, 0, 0, alpha), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                sprites.Draw(buttonImage, new Vector2((width - buttonImage.Width) / 2, height - 60 - buttonImage.Height)
                    , null, new Color(alpha, alpha, alpha, alpha), 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                sprites.End();
            }

            base.Draw(gameTime);
        }

        public void Show()
        {
            isHidden = false;
            startFillingAlpha = true;
            startDrainingAlpha = false;
        }

        public void Hide()
        {
            isHidden = false;
            startDrainingAlpha = true;
            startFillingAlpha = false;
        }
    }
}